package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Systemdictionary;
import cn.wolfcode.domain.Systemdictionaryitem;
import cn.wolfcode.qo.SystemDictionaryItemQuery;
import cn.wolfcode.service.ISystemdictionaryService;
import cn.wolfcode.service.ISystemdictionaryitemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemdictionaryitemController {

    @Autowired
    private ISystemdictionaryitemService systemdictionaryitemService;
    @Autowired
    private ISystemdictionaryService systemdictionaryService;


    @RequiresPermissions(value = {"systemdictionaryitem:list","权限页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQuery qo){
        PageInfo<Systemdictionaryitem> pageInfo = systemdictionaryitemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询所有目录
        List<Systemdictionary> systemdictionaries = systemdictionaryService.listAll();
        model.addAttribute("systemdictionaries",systemdictionaries);
        return "systemdictionaryitem/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemdictionaryitem:delete","权限删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            systemdictionaryitemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemdictionaryitem:saveOrUpdate","权限新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Systemdictionaryitem systemdictionaryitem){
        if (systemdictionaryitem.getId() != null) {
            systemdictionaryitemService.update(systemdictionaryitem);
        }else {
            systemdictionaryitemService.save(systemdictionaryitem);
        }
        return new JsonResult();
    }
}
